package com.hns.frame.security;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import com.hns.frame.enumrate.RequestResult;
import com.hns.frame.util.ResponseUtil;

/**
 * 手机端登录错误处理
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class PhoneAuthenticationFailureHandler implements AuthenticationFailureHandler {  
    static final Logger logger = LoggerFactory.getLogger(PhoneAuthenticationFailureHandler.class);
  
    public PhoneAuthenticationFailureHandler() {  
    }  
  
    /**
     * 失败返回的json
     * @param request
     * @param response
     * @param exception
     * @throws IOException
     * @throws ServletException
     * @see org.springframework.security.web.authentication.AuthenticationFailureHandler#onAuthenticationFailure(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.AuthenticationException)
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,  
            AuthenticationException exception) throws IOException, ServletException { 
        Map<String,String> map = new HashMap<String,String>();
        map.put("msgType", RequestResult.FAIL.getFlag());
        //TODO 待改动返回登录失败具体信息
        map.put("msg", "登录失败，用户名或密码错误");
        ResponseUtil.responseJSON(map, response);
    }  
  
} 